﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIOCUR.Models;

namespace SIOCUR.Controllers
{
    public class EquipoAuxiliarController : Controller
    {
        SIOCURRepository dbpRepository = new SIOCURRepository();
        //
        // GET: /EquipoAuxiliar/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Registra()
        {
            return View("Registra");
        }

        [HttpPost]
        public ActionResult Registra(FormCollection formValues, EQUIPO_AUXILIAR eqAux)
        {
            String estacion = Session["sesionestacion"] as string;
            String sistema = Session["sesionsistema"] as string;

            if (ModelState.IsValid)
            {
                IQueryable<EQUIPO_AUXILIAR> Aux = dbpRepository.VerificarEqAux(estacion, sistema);

                if (Aux.Count() == 0)
                {
                    dbpRepository.AddEqAux(estacion, sistema, DateTime.Now, Request.Form[0], Request.Form[1], Request.Form[2], Request.Form[3], Request.Form[4]);
                    var equAuxi = dbpRepository.FindAllEqAuxiliar().ToList();
                    return View("Agregado", equAuxi);
                }
                else
                {
                    return View("ErrorGuardar");
                }
                               
            }
            else
            {
                return View();
            }
            //return View();
        }

        public ActionResult Todos()
        {
            var equAuxi = dbpRepository.FindAllEqAuxiliar().ToList();
            return View(equAuxi);
        }

        public ActionResult Modifica()
        {
            String estacion = Session["sesionestacion"] as string;
            String sistema = Session["sesionsistema"] as string;

            return View(dbpRepository.GetEqAuxiliars(estacion, sistema));
        }
        
        public ActionResult Modificar(int id)
        {
            EQUIPO_AUXILIAR ea = dbpRepository.GetEqAuxiliar(id);
            return View(ea);

        }

        [HttpPost]
        public ActionResult Modificar(int id, FormCollection formValues)
        {
            EQUIPO_AUXILIAR ea = dbpRepository.GetEqAuxiliar(id);
            if (TryUpdateModel(ea))
            {
                dbpRepository.Save();
                return RedirectToAction("Modifica");
            }
            return View("Modifica");
        }

    }
}
